package net.gyapp.markim.module.contact;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.easyder.wrapper.base.view.WrapperSwipeActivity;
import com.easyder.wrapper.core.manager.ImageManager;
import com.easyder.wrapper.core.model.BaseVo;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import net.gyapp.markim.R;
import net.gyapp.markim.core.entity.ConversationEntity;
import net.gyapp.markim.module.basic.base.CommonPresenter;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import me.winds.widget.autolayout.utils.AutoUtils;
import me.winds.widget.usage.TitleView;

/**
 * Auther:  winds
 * Email:   heardown@163.com
 * Date:    2018/8/5.
 * Desc:
 */
public class GroupQrActivity extends WrapperSwipeActivity<CommonPresenter> {

    @BindView(R.id.iv_header)
    ImageView ivHeader;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.iv_grade)
    ImageView ivGrade;
    @BindView(R.id.tv_id)
    TextView tvId;
    @BindView(R.id.layout_info)
    LinearLayout layoutInfo;
    @BindView(R.id.iv_qr_code)
    ImageView ivQrCode;

    public static Intent getIntent(Context context, ConversationEntity entity, String qr) {
        return new Intent(context, GroupQrActivity.class).putExtra("entity", entity).putExtra("qr", qr);
    }

    @Override
    protected int getViewLayout() {
        return R.layout.activity_v_card;
    }

    @Override
    protected void initView(Bundle savedInstanceState, TitleView titleView, Intent intent) {
        titleView.setCenterText("群二维码");
        ConversationEntity entity = (ConversationEntity) intent.getSerializableExtra("entity");
        ImageManager.load(mActivity, ivHeader, entity.avatar, R.drawable.placeholder_group_header);
        tvName.setText(entity.name);
        tvId.setText(String.format("%1$s%2$s", "ID:", entity.sessionIdStr));
    }

    @Override
    protected void loadData(Bundle savedInstanceState, Intent intent) {
        String qr = "group:" + intent.getStringExtra("qr");
        ivQrCode.setImageBitmap(generateQRIamge(qr, AutoUtils.getPercentWidthSize(650), AutoUtils.getPercentWidthSize(650)));
    }

    @Override
    public void showContentView(String url, BaseVo dataVo) {

    }

    private Bitmap generateQRIamge(String QRCode, int height, int width) {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();

        Map<EncodeHintType, String> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        try {
            BitMatrix encode = qrCodeWriter.encode(QRCode, BarcodeFormat.QR_CODE, width, height, hints);
            int[] pixels = new int[width * height];
            for (int i = 0; i < height; i++) {
                for (int j = 0; j < width; j++) {
                    if (encode.get(j, i)) {
                        pixels[i * width + j] = 0x00000000;
                    } else {
                        pixels[i * width + j] = 0xffffffff;
                    }
                }
            }
            return Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        return null;
    }
}
